using System;
using System.Collections.Generic;
using System.Linq;
using FireStarter.Base;
using Firestarter.DB.Schema.BO;
using FireStarterModeller.BO;
using FireStarterModeller.Test;
using FireStarterModeller.Test.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;
 
using Rhino.Mocks;

namespace Firestarter.DB_DomainModel.Logic.Test.DBChangesImporterTests
{
    [TestFixture]
    public class TestDBChangesImporter_ChangeTypeIsAdd : TestBase
    {
        [Test]
        public void Test_ImportDB_NewDBTables_ShouldAddClassesToSolution()
        {
            //---------------Set up test pack-------------------
            IDBDatabase database = new DBDatabase();
            database.Tables.Add(new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd());
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
            Assert.That(solution.DefaultAssembly, Is.Not.Null);
            Assert.That(solution.Assemblies.Contains(solution.DefaultAssembly));

            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);

            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(database.Tables.Count));
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
            database.Tables[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        [Test]
        public void Test_ImportDB_NewDBTables_ShouldNotAddUnapproved()
        {
            DBDatabase database = new DBDatabase();
            database.Tables.Add(new DBTable(TestUtilsShared.GetRandomString()).UnApproveForAdd());
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));

            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);

            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            database.Tables[0].AssertIsUnApprovedAndPendingChangeTypeIs(ChangeType.Add);
        }

        [Test]
        public void Test_ImportDB_NewDBProps_ShouldAddPropertiesToClass()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString());
            database.Tables.Add(table.ApproveForAdd());
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().Column.ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();

            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
            Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
            Assert.That(solution.DefaultAssembly, Is.Not.Null);

            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);

            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(database.Tables.Count));
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(database.Tables[0].Columns.Count));
            table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        [Test]
        public void Test_ImportDB_NewDBProps_ShouldNotAddUnapproved()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().Column.UnApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();

            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
            Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
            Assert.That(solution.DefaultAssembly, Is.Not.Null);

            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);

            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
            table.Columns[0].AssertIsUnApprovedAndPendingChangeTypeIs(ChangeType.Add);
        }

        [Test]
        public void Test_ImportDB_NewDBProps_WhenUnChangedTableHasNoMappedClass_ShouldNotAddApprovedColumns()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForNone();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            DBColumn column = builder.AddPrimaryKeyColumn().Column.ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
            Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
            Assert.That(solution.DefaultAssembly, Is.Not.Null);
            //---------------Execute Test ----------------------
            IList<IBOError> errors = importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.IsNotNull(errors);
            Assert.AreEqual(2, errors.Count);
            IBOError error = errors[0];
            Assert.AreSame(column, error.BusinessObject);
            Assert.AreEqual(ErrorLevel.Warning, error.Level);
            StringAssert.Contains(column.ColumnName, error.Message);
            StringAssert.Contains(string.Format("the table '{0}' has no matching class", table.TableName), error.Message);
        }

        [Test]
        public void Test_ImportDB_NewDBProps_WhenTableAddIsNotApproved_ShouldNotAddApprovedColumns()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).UnApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().Column.ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
            Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
            Assert.That(solution.DefaultAssembly, Is.Not.Null);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
        }

        // When Unapproved Table Update, should add Approved Cols
        [Test]
        public void Test_ImportDB_NewDBProps_WhenTableUpdateIsNotApproved_ShouldAddApprovedColumn()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString());
            database.Tables.Add(table.ApproveForAdd());
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            importer.ImportDBChanges(database, solution);
            table.UnApproveForUpdate();
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().Column.ApproveForAdd();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(1));
            table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        // When Unapproved Table Delete, should add Approved Cols
        [Test]
        public void Test_ImportDB_NewDBProps_WhenTableDeleteIsNotApproved_ShouldAddApprovedColumn()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString());
            database.Tables.Add(table.ApproveForAdd());
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            importer.ImportDBChanges(database, solution);
            table.UnApproveForDelete();
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().Column.ApproveForAdd();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(1));
            table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        // When Unapproved Table Rename, should add Approved Cols
        [Test]
        public void Test_ImportDB_NewDBProps_WhenTableRenameIsNotApproved_ShouldAddApprovedColumn()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString());
            database.Tables.Add(table.ApproveForAdd());
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            importer.ImportDBChanges(database, solution);
            table.UnApproveForRename();
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().Column.ApproveForAdd();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(1));
            table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        // When Unapproved Table None, should add Approved Cols
        [Test]
        public void Test_ImportDB_NewDBProps_WhenTableNoneIsNotApproved_ShouldAddApprovedColumn()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString());
            database.Tables.Add(table.ApproveForAdd());
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            importer.ImportDBChanges(database, solution);
            table.UnApproveForNone();
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().Column.ApproveForAdd();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(1));
            table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        [Test]
        public void Test_ImportDB_NewDBPrimaryKey_ShouldAddObjectIdentityToClass()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().ApproveForAdd().Column.ApproveForAdd();
            DBPrimaryKey dbPrimaryKey = table.PrimaryKeyDB.ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            dbPrimaryKey.AssertIsApprovedAndPendingChangeTypeIs(ChangeType.Add);
            dbPrimaryKey.PrimaryKeyColumns[0].AssertIsApprovedAndPendingChangeTypeIs(ChangeType.Add);
            dbPrimaryKey.PrimaryKeyColumns[0].Column.AssertIsApprovedAndPendingChangeTypeIs(ChangeType.Add);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(database.Tables.Count));
            DMClass dmClass = solution.AllClasses[0];
            DMObjectIdentity objectIdentity = dmClass.ObjectIdentity;
            Assert.That(objectIdentity, Is.Not.Null);
            Assert.That(objectIdentity.Properties.Contains(dmClass.Properties[0]));
            dbPrimaryKey.AssertIsBackToApprovedAndPendingChangeTypeIsNone();
            dbPrimaryKey.PrimaryKeyColumns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        [Test]
        public void Test_ImportDB_NewDBPrimaryKey_ShouldNotAddUnapproved()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().ApproveForAdd();
            table.PrimaryKeyDB.UnApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].ObjectIdentity, Is.Null);
            table.PrimaryKeyDB.AssertIsUnApprovedAndPendingChangeTypeIs(ChangeType.Add);
        }

        // Story: The following tests need to be repeated for each of PK/UK/FK where something they are dependant on has not been approved for add or do not exist in the domain model.

        //[Test]
        //public void Test_ImportDB_NewDBPrimaryKey_WhenUnChangedTableHasNoMappedClass_ShouldNotAddApprovedColumns()
        //{
        //    //---------------Set up test pack-------------------
        //    DBDatabase database = new DBDatabase();
        //    DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForNone();
        //    database.Tables.Add(table);
        //    DBTableBuilder builder = new DBTableBuilder(table);
        //    DBColumn column = builder.AddPrimaryKeyColumn().ApproveForAdd();
        //    DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
        //    IDBTypeToDMPropertyTypeMapper typeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
        //    DBChangesImporter importer = new DBChangesImporter();
        //    //---------------Assert Precondition----------------
        //    Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
        //    Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
        //    Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
        //    Assert.That(solution.DefaultAssembly, Is.Not.Null);
        //    //---------------Execute Test ----------------------
        //    IList<BOError> errors = importer.ImportDBChanges(database, solution);
        //    //---------------Test Result -----------------------
        //    Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
        //    Assert.IsNotNull(errors);
        //    Assert.AreEqual(1, errors.Count);
        //    BOError error = errors[0];
        //    Assert.AreSame(column, error.BusinessObject);
        //    Assert.AreEqual(ErrorLevel.Warning, error.Level);
        //    StringAssert.Contains(column.ColumnName, error.Message);
        //    StringAssert.Contains(string.Format("the table '{0}' has no matching class", table.TableName), error.Message);
        //}

        //[Test]
        //public void Test_ImportDB_NewDBPrimaryKey_WhenTableAddIsNotApproved_ShouldNotAddApprovedBPrimaryKey()
        //{
        //    //---------------Set up test pack-------------------
        //    DBDatabase database = new DBDatabase();
        //    DBTable table = new DBTable(TestUtilsShared.GetRandomString()).UnApproveForAdd();
        //    database.Tables.Add(table);
        //    DBTableBuilder builder = new DBTableBuilder(table);
        //    builder.AddPrimaryKeyColumn().ApproveForAdd();
        //    DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
        //    IDBTypeToDMPropertyTypeMapper typeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
        //    DBChangesImporter importer = new DBChangesImporter();
        //    //---------------Assert Precondition----------------
        //    Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
        //    Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
        //    Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
        //    Assert.That(solution.DefaultAssembly, Is.Not.Null);
        //    //---------------Execute Test ----------------------
        //    importer.ImportDBChanges(database, solution);
        //    //---------------Test Result -----------------------
        //    Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
        //}

        //// When Unapproved Table Update, should add Approved Cols
        //[Test]
        //public void Test_ImportDB_NewDBPrimaryKey_WhenTableUpdateIsNotApproved_ShouldAddApprovedBPrimaryKey()
        //{
        //    //---------------Set up test pack-------------------
        //    DBDatabase database = new DBDatabase();
        //    DBTable table = new DBTable(TestUtilsShared.GetRandomString());
        //    database.Tables.Add(table.ApproveForAdd());
        //    DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
        //    IDBTypeToDMPropertyTypeMapper typeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
        //    DBChangesImporter importer = new DBChangesImporter();
        //    importer.ImportDBChanges(database, solution);
        //    table.UnApproveForUpdate();
        //    DBTableBuilder builder = new DBTableBuilder(table);
        //    builder.AddPrimaryKeyColumn().ApproveForAdd();
        //    //---------------Assert Precondition----------------
        //    Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
        //    //---------------Execute Test ----------------------
        //    importer.ImportDBChanges(database, solution);
        //    //---------------Test Result -----------------------
        //    Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(1));
        //    table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        //}

        //// When Unapproved Table Delete, should add Approved Cols
        //[Test]
        //public void Test_ImportDB_NewDBPrimaryKey_WhenTableDeleteIsNotApproved_ShouldAddApprovedBPrimaryKey()
        //{
        //    //---------------Set up test pack-------------------
        //    DBDatabase database = new DBDatabase();
        //    DBTable table = new DBTable(TestUtilsShared.GetRandomString());
        //    database.Tables.Add(table.ApproveForAdd());
        //    DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
        //    IDBTypeToDMPropertyTypeMapper typeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
        //    DBChangesImporter importer = new DBChangesImporter();
        //    importer.ImportDBChanges(database, solution);
        //    table.UnApproveForDelete();
        //    DBTableBuilder builder = new DBTableBuilder(table);
        //    builder.AddPrimaryKeyColumn().ApproveForAdd();
        //    //---------------Assert Precondition----------------
        //    Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
        //    //---------------Execute Test ----------------------
        //    importer.ImportDBChanges(database, solution);
        //    //---------------Test Result -----------------------
        //    Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(1));
        //    table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        //}

        //// When Unapproved Table Rename, should add Approved Cols
        //[Test]
        //public void Test_ImportDB_NewDBPrimaryKey_WhenTableRenameIsNotApproved_ShouldAddApprovedBPrimaryKey()
        //{
        //    //---------------Set up test pack-------------------
        //    DBDatabase database = new DBDatabase();
        //    DBTable table = new DBTable(TestUtilsShared.GetRandomString());
        //    database.Tables.Add(table.ApproveForAdd());
        //    DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
        //    IDBTypeToDMPropertyTypeMapper typeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
        //    DBChangesImporter importer = new DBChangesImporter();
        //    importer.ImportDBChanges(database, solution);
        //    table.UnApproveForRename();
        //    DBTableBuilder builder = new DBTableBuilder(table);
        //    builder.AddPrimaryKeyColumn().ApproveForAdd();
        //    //---------------Assert Precondition----------------
        //    Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
        //    //---------------Execute Test ----------------------
        //    importer.ImportDBChanges(database, solution);
        //    //---------------Test Result -----------------------
        //    Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(1));
        //    table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        //}

        //// When Unapproved Table None, should add Approved Cols
        //[Test]
        //public void Test_ImportDB_NewDBPrimaryKey_WhenTableNoneIsNotApproved_ShouldAddApprovedBPrimaryKey()
        //{
        //    //---------------Set up test pack-------------------
        //    DBDatabase database = new DBDatabase();
        //    DBTable table = new DBTable(TestUtilsShared.GetRandomString());
        //    database.Tables.Add(table.ApproveForAdd());
        //    DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
        //    IDBTypeToDMPropertyTypeMapper typeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
        //    DBChangesImporter importer = new DBChangesImporter();
        //    importer.ImportDBChanges(database, solution);
        //    table.UnApproveForNone();
        //    DBTableBuilder builder = new DBTableBuilder(table);
        //    builder.AddPrimaryKeyColumn().ApproveForAdd();
        //    //---------------Assert Precondition----------------
        //    Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
        //    //---------------Execute Test ----------------------
        //    importer.ImportDBChanges(database, solution);
        //    //---------------Test Result -----------------------
        //    Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(1));
        //    table.Columns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        //}

        [Test]
        public void Test_ImportDB_NewDBKey_ShouldAddUniqueConstraintToClass()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddUniqueKeyColumn("ExistingAKColumn").ApproveForAdd();
            DBKey dbKey = table.Keys[0].ApproveForAdd();
            table.Keys[0].KeyColumns[0].ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, table.Columns.Count);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(database.Tables.Count));
            DMClass dmClass = solution.AllClasses[0];
            Assert.AreEqual(1, dmClass.UniqueConstraints.Count);
            DMUniqueConstraint uniqueConstraint = dmClass.UniqueConstraints[0];
            Assert.That(uniqueConstraint, Is.Not.Null);
            Assert.That(uniqueConstraint.Properties.Contains(dmClass.Properties[0]));
            dbKey.AssertIsBackToApprovedAndPendingChangeTypeIsNone();
            dbKey.KeyColumns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        [Test]
        public void Test_ImportDB_NewDBKey_ShouldNotAddUnapproved()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddUniqueKeyColumn("ExistingAKColumn").ApproveForAdd();
            DBKey dbKey = table.Keys[0].UnApproveForAdd();
            table.Keys[0].KeyColumns[0].ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, table.Columns.Count);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].UniqueConstraints.Count, Is.EqualTo(0));
            dbKey.AssertIsUnApprovedAndPendingChangeTypeIs(ChangeType.Add);
        }

        [Test]
        public void Test_ImportDB_NewDBForeignKey_ShouldAddRelationshipToClass()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            string tableName1 = TestUtilsShared.GetRandomString();
            DBTable table1 = new DBTable(tableName1).ApproveForAdd();
            database.Tables.Add(table1);
            string tableName2 = TestUtilsShared.GetRandomString();
            DBTable table2 = new DBTable(tableName2).ApproveForAdd();
            database.Tables.Add(table2);

            DBTableBuilder builder1 = new DBTableBuilder(table1);
            builder1.AddPrimaryKeyColumn().ApproveForAdd();
            DBTableBuilder builder2 = new DBTableBuilder(table2);
            DBForeignKey foreignKey = builder2.AddForeignKey(TestUtilsShared.GetRandomString(), table1).ApproveForAdd();
            var dbForeignKeyColumn = foreignKey.ForeignKeyColumns[0];
            dbForeignKeyColumn.ApproveForAdd().ParentColumn.ApproveForAdd();
            dbForeignKeyColumn.ApproveForAdd().RelatedColumn.ApproveForAdd();

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, foreignKey.ForeignKeyColumns.Count);
            Assert.AreEqual(ChangeType.Add, dbForeignKeyColumn.ParentColumn.PendingChangeType);
            Assert.AreEqual(ChangeType.Add, dbForeignKeyColumn.RelatedColumn.PendingChangeType);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(database.Tables.Count));
            DMClass dmClass1 = solution.AllClasses.Find(class1 => class1.MappedTableName == tableName1);
            DMClass dmClass2 = solution.AllClasses.Find(class1 => class1.MappedTableName == tableName2);
            Assert.AreEqual(1, dmClass2.OwnerRelationships.Count);
            Assert.AreEqual(1, dmClass1.RelatedRelationships.Count);
            DMRelationship relationship = dmClass2.OwnerRelationships[0];
            Assert.AreEqual(Cardinality.Single, relationship.Cardinality);
            Assert.AreEqual(1, relationship.RelationshipProperties.Count);
            foreignKey.AssertIsBackToApprovedAndPendingChangeTypeIsNone();
            foreignKey.ForeignKeyColumns[0].AssertIsBackToApprovedAndPendingChangeTypeIsNone();
        }

        [Test]
        public void Test_ImportDB_NewDBForeignKey_NewRelAndRevRel_ShouldGenerateCodeTrue()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            string tableName1 = TestUtilsShared.GetRandomString();
            DBTable table1 = new DBTable(tableName1).ApproveForAdd();
            database.Tables.Add(table1);
            string tableName2 = TestUtilsShared.GetRandomString();
            DBTable table2 = new DBTable(tableName2).ApproveForAdd();
            database.Tables.Add(table2);

            DBTableBuilder builder1 = new DBTableBuilder(table1);
            builder1.AddPrimaryKeyColumn().ApproveForAdd();
            DBTableBuilder builder2 = new DBTableBuilder(table2);
            DBForeignKey foreignKey = builder2.AddForeignKey(TestUtilsShared.GetRandomString(), table1).ApproveForAdd();
            foreignKey.ForeignKeyColumns[0].ApproveForAdd().ParentColumn.ApproveForAdd();

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(database.Tables.Count));
            DMClass dmClass1 = solution.AllClasses.Find(class1 => class1.MappedTableName == tableName1);
            DMClass dmClass2 = solution.AllClasses.Find(class1 => class1.MappedTableName == tableName2);

            DMRelationship relationship = dmClass2.OwnerRelationships[0];
            Assert.IsTrue(relationship.GenerateCode);
            var reverseRel = dmClass1.RelationshipsDM.First();
            Assert.IsTrue(reverseRel.GenerateCode);
        }

        [Test]
        public void Test_ImportDB_NewDBForeignKey_ShouldNotAddUnapproved()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            string tableName1 = TestUtilsShared.GetRandomString();
            DBTable table1 = new DBTable(tableName1).ApproveForAdd();
            database.Tables.Add(table1);
            string tableName2 = TestUtilsShared.GetRandomString();
            DBTable table2 = new DBTable(tableName2).ApproveForAdd();
            database.Tables.Add(table2);

            DBTableBuilder builder1 = new DBTableBuilder(table1);
            builder1.AddPrimaryKeyColumn().ApproveForAdd();
            DBTableBuilder builder2 = new DBTableBuilder(table2);
            DBForeignKey foreignKey = builder2.AddForeignKey(TestUtilsShared.GetRandomString(), table1).UnApproveForAdd();
            foreignKey.ForeignKeyColumns[0].ApproveForAdd().ParentColumn.ApproveForAdd();

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            DMClass dmClass2 = solution.AllClasses.Find(class1 => class1.MappedTableName == tableName2);
            Assert.That(dmClass2.OwnerRelationships.Count, Is.EqualTo(0));
            foreignKey.AssertIsUnApprovedAndPendingChangeTypeIs(ChangeType.Add);
        }

        [Test]
        public void Test_ImportDB_NewDBColumnWithLength_WhenStringProp_ShouldAddMaxLengthParameterToDefaultPropertyRule()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            int dataLength = TestUtilsShared.GetRandomInt(50, 255);
            string dataType = TestUtilsShared.GetRandomString();
            DBColumn dbColumn = builder.AddColumn(TestUtilsShared.GetRandomString(), dataType, dataLength);
            dbColumn.PendingChangeApproved = true;
            dbColumn.PendingChangeType = ChangeType.Add;

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            IDBTypeToDMPropertyTypeMapper typeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
            typeMapper.Stub(t => t.GetDMPropertyType(dataType, dataLength)).Return(new DMPropertyType("System", "String"));
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter(typeMapper);
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
            Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
            Assert.That(solution.DefaultAssembly, Is.Not.Null);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);

            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(database.Tables.Count));
            DMClass dmClass = solution.AllClasses[0];
            Assert.That(dmClass.Properties.Count, Is.EqualTo(database.Tables[0].Columns.Count));
            DMProperty property = dmClass.Properties[0];
            Assert.IsNotNull(property.DefaultPropRule);
            DMPropRuleParameter maxLengthParameter = property.DefaultPropRule.PropRuleParameters.Find(parameter => parameter.KeyName == "maxLength");
            Assert.IsNotNull(maxLengthParameter);
            Assert.AreEqual(dataLength.ToString(), maxLengthParameter.Value);
        }

        [Test]
        public void Test_ImportDB_NewDBColumnWithLength_WhenNotStringProp_ShouldNotAddMaxLengthParameterToDefaultPropertyRule()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            int dataLength = TestUtilsShared.GetRandomInt(50, 255);
            string dataType = TestUtilsShared.GetRandomString();
            DBColumn dbColumn = builder.AddColumn(TestUtilsShared.GetRandomString(), dataType, dataLength);
            dbColumn.PendingChangeApproved = true;
            dbColumn.PendingChangeType = ChangeType.Add;

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            IDBTypeToDMPropertyTypeMapper typeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
            typeMapper.Stub(t => t.GetDMPropertyType(dataType, dataLength)).Return(new DMPropertyType());
            DBChangesImporter importer = TestUtilsDBChangesImporter.CreateDBChangesImporter(typeMapper);
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
            Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
            Assert.That(solution.DefaultAssembly, Is.Not.Null);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);

            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(database.Tables.Count));
            DMClass dmClass = solution.AllClasses[0];
            Assert.That(dmClass.Properties.Count, Is.EqualTo(database.Tables[0].Columns.Count));
            DMProperty property = dmClass.Properties[0];
            Assert.IsNotNull(property.DefaultPropRule);
            DMPropRuleParameter maxLengthParameter = property.DefaultPropRule.PropRuleParameters.Find(parameter => parameter.KeyName == "maxLength");
            Assert.IsNull(maxLengthParameter);
        }

        [Test]
        public void Test_ImportDB_NewDBPrimaryKey_WhenSuccessful_ShouldReturnNoErrors()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().ApproveForAdd();
            DBPrimaryKey dbPrimaryKey = table.PrimaryKeyDB.ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();

            List<string> errorMessages = new List<string>();
            IDBToDMImporterFactory dbToDMImporterFactory = TestUtilsDBChangesImporter.CreateStandardImporterFactoryStub();
            TestUtilsDBChangesImporter.SetupFactoryWithObjectIdentityImporterErrorMessages(dbToDMImporterFactory, dbPrimaryKey, errorMessages);
            DBChangesImporter importer = new DBChangesImporter(dbToDMImporterFactory);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, errorMessages.Count);
            //---------------Execute Test ----------------------
            IList<IBOError> errors = importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.AreEqual(0, errors.Count);
        }

        [Test]
        public void Test_ImportDB_NewDBPrimaryKey_WhenHasErrors_ShouldReturnErrorsAndAbortChange()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().ApproveForAdd();
            DBPrimaryKey dbPrimaryKey = table.PrimaryKeyDB.ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();

            List<string> errorMessages = new List<string> { TestUtilsShared.GetRandomString() };
            IDBToDMImporterFactory dbToDMImporterFactory = TestUtilsDBChangesImporter.CreateStandardImporterFactoryStub();
            TestUtilsDBChangesImporter.SetupFactoryWithObjectIdentityImporterErrorMessages(dbToDMImporterFactory, dbPrimaryKey, errorMessages);
            DBChangesImporter importer = new DBChangesImporter(dbToDMImporterFactory);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, errorMessages.Count);
            //---------------Execute Test ----------------------
            IList<IBOError> errors = importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(errors.Count, Is.EqualTo(1));
            Assert.That(errors[0].BusinessObject, Is.SameAs(dbPrimaryKey));
            Assert.That(errors[0].Message, Is.EqualTo(errorMessages[0]));
            Assert.That(errors[0].Level, Is.EqualTo(ErrorLevel.Error));
            dbPrimaryKey.AssertIsApprovedAndPendingChangeTypeIs(ChangeType.Add);
        }

        [Test]
        public void Test_ImportDB_NewDBKey_WhenSuccessful_ShouldReturnNoErrors()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddUniqueKeyColumn("ExistingAKColumn").ApproveForAdd();
            DBKey dbKey = table.Keys[0].ApproveForAdd();
            table.Keys[0].KeyColumns[0].ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();

            List<string> errorMessages = new List<string>();
            IDBToDMImporterFactory dbToDMImporterFactory = TestUtilsDBChangesImporter.CreateStandardImporterFactoryStub();
            TestUtilsDBChangesImporter.SetupFactoryWithUniqueConstraintImporterErrorMessages(dbToDMImporterFactory, dbKey, errorMessages);
            DBChangesImporter importer = new DBChangesImporter(dbToDMImporterFactory);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, errorMessages.Count);
            //---------------Execute Test ----------------------
            IList<IBOError> errors = importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.AreEqual(0, errors.Count);
        }

        [Test]
        public void Test_ImportDB_NewDBKey_WhenHasErrors_ShouldReturnErrors()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddUniqueKeyColumn("ExistingAKColumn").ApproveForAdd();
            DBKey dbKey = table.Keys[0].ApproveForAdd();
            table.Keys[0].KeyColumns[0].ApproveForAdd();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();

            List<string> errorMessages = new List<string>{TestUtilsShared.GetRandomString()};
            IDBToDMImporterFactory dbToDMImporterFactory = TestUtilsDBChangesImporter.CreateStandardImporterFactoryStub();
            TestUtilsDBChangesImporter.SetupFactoryWithUniqueConstraintImporterErrorMessages(dbToDMImporterFactory, dbKey, errorMessages);
            DBChangesImporter importer = new DBChangesImporter(dbToDMImporterFactory);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, errorMessages.Count);
            //---------------Execute Test ----------------------
            IList<IBOError> errors = importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, errors.Count);
            Assert.That(errors[0].BusinessObject, Is.SameAs(dbKey));
            Assert.That(errors[0].Message, Is.EqualTo(errorMessages[0]));
            Assert.That(errors[0].Level, Is.EqualTo(ErrorLevel.Error));
            dbKey.AssertIsApprovedAndPendingChangeTypeIs(ChangeType.Add);
        }

        [Test]
        public void Test_ImportDB_NewDBForeignKey_WhenSuccessful_ShouldReturnNoErrors()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            string tableName1 = TestUtilsShared.GetRandomString();
            DBTable table1 = new DBTable(tableName1).ApproveForAdd();
            database.Tables.Add(table1);
            string tableName2 = TestUtilsShared.GetRandomString();
            DBTable table2 = new DBTable(tableName2).ApproveForAdd();
            database.Tables.Add(table2);

            DBTableBuilder builder1 = new DBTableBuilder(table1);
            builder1.AddPrimaryKeyColumn().ApproveForAdd();
            DBTableBuilder builder2 = new DBTableBuilder(table2);
            DBForeignKey foreignKey = builder2.AddForeignKey(TestUtilsShared.GetRandomString(), table1).ApproveForAdd();
            foreignKey.ForeignKeyColumns[0].ApproveForAdd().ParentColumn.ApproveForAdd();

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();

            List<string> errorMessages = new List<string>();
            IDBToDMImporterFactory dbToDMImporterFactory = TestUtilsDBChangesImporter.CreateStandardImporterFactoryStub();
            TestUtilsDBChangesImporter.SetupFactoryWithRelationshipImporterErrorMessages(dbToDMImporterFactory, foreignKey, errorMessages);
            DBChangesImporter importer = new DBChangesImporter(dbToDMImporterFactory);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, errorMessages.Count);
            //---------------Execute Test ----------------------
            IList<IBOError> errors = importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.AreEqual(0, errors.Count);
        }

        [Test]
        public void Test_ImportDB_NewDBForeignKey_WhenHasErrors_ShouldReturnErrors()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            string tableName1 = TestUtilsShared.GetRandomString();
            DBTable table1 = new DBTable(tableName1).ApproveForAdd();
            database.Tables.Add(table1);
            string tableName2 = TestUtilsShared.GetRandomString();
            DBTable table2 = new DBTable(tableName2).ApproveForAdd();
            database.Tables.Add(table2);

            DBTableBuilder builder1 = new DBTableBuilder(table1);
            builder1.AddPrimaryKeyColumn().ApproveForAdd();
            DBTableBuilder builder2 = new DBTableBuilder(table2);
            DBForeignKey foreignKey = builder2.AddForeignKey(TestUtilsShared.GetRandomString(), table1).ApproveForAdd();
            foreignKey.ForeignKeyColumns[0].ApproveForAdd().ParentColumn.ApproveForAdd();

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();

            List<string> errorMessages = new List<string>{TestUtilsShared.GetRandomString()};
            IDBToDMImporterFactory dbToDMImporterFactory = TestUtilsDBChangesImporter.CreateStandardImporterFactoryStub();
            TestUtilsDBChangesImporter.SetupFactoryWithRelationshipImporterErrorMessages(dbToDMImporterFactory, foreignKey, errorMessages);
            DBChangesImporter importer = new DBChangesImporter(dbToDMImporterFactory);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, errorMessages.Count);
            //---------------Execute Test ----------------------
            IList<IBOError> errors = importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, errors.Count);
            Assert.That(errors[0].BusinessObject, Is.SameAs(foreignKey));
            Assert.That(errors[0].Message, Is.EqualTo(errorMessages[0]));
            Assert.That(errors[0].Level, Is.EqualTo(ErrorLevel.Error));
            foreignKey.AssertIsApprovedAndPendingChangeTypeIs(ChangeType.None);
        }
    }
}